import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/bank_card_list_vo.dart';
import 'package:xinxuan/vo/common_vo/common_bank_card_item_vo.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/withdrawal_list_vo.dart';

class WithdrawalPageController extends GetxController {
  
  final CommonPageController commonPageController = Get.find();
  
  String verificationKey = '';
  
  /// 提现记录
  var withdrawalListVo = WithdrawalListVo(
    data: [], 
    meta: WithdrawalMeta(
      assets: WithdrawalAssets(
        available: '0.00',
        withdraw: '0.00'
      ),
      pagination: CommonPagination(
        currentPage: 1
      )
    )
  ).obs;

  /// 提现银行卡列表
  var bankCardList = <CommonBankCardItemVo>[].obs;
  /// 银行卡下拉选中项
  var scrollController = FixedExtentScrollController( initialItem: 0).obs;
  /// 选中的银行卡
  var selectedBankItem = CommonBankCardItemVo(id: 0).obs;
  String amount = '';
  String verificationCode = '';

  @override
  void onReady() {
    getWithdrawalRecordList();
    getBankCardList();
    super.onReady();
  }

  /// 获取提现银行卡列表
  getBankCardList() async {
    BankCardListVo bankCardListVo = await HttpUtilService.getBankCardList(type: 2, loading: false);
    bankCardList.value = bankCardListVo.data!;
    update();
  }

  /// 获取提现记录
  getWithdrawalRecordList() async {
    withdrawalListVo.value.meta!.pagination!.currentPage = 1;
    WithdrawalListVo tempVo = await HttpUtilService.getWithdrawalRecordList(page: withdrawalListVo.value.meta!.pagination!.currentPage!);
    withdrawalListVo.value = tempVo;
    update();
  }

  /// 加载更多提现记录
  onLoadWithdrawalRecordList() async {
    if (withdrawalListVo.value.meta!.pagination!.total! <= withdrawalListVo.value.data!.length) {
      return;
    }
    withdrawalListVo.value.meta!.pagination!.currentPage = withdrawalListVo.value.meta!.pagination!.currentPage! + 1;
    WithdrawalListVo tempVo = await HttpUtilService.getWithdrawalRecordList(page: withdrawalListVo.value.meta!.pagination!.currentPage!);
    withdrawalListVo.value.data!.addAll(tempVo.data!);
    update();
  }

  /// 修改选中银行卡
  changeBankIndex(int index) {
    scrollController.value = FixedExtentScrollController( initialItem: index);
  }

  /// 确定选中银行卡
  determineBank() {
    selectedBankItem.value = bankCardList[scrollController.value.initialItem];
    update();
  }

  /// 提交申请
  withdrawal() async {
    if (TextUtil.isEmpty(amount)) {
      EasyLoading.showToast('请输入提现金额');
      return;
    }
    if (TextUtil.isEmpty(verificationCode)) {
      EasyLoading.showToast('请输入验证码');
      return;
    }
    if (selectedBankItem.value.id! <= 0) {
      EasyLoading.showToast('请选择银行卡');
      return;
    }
    bool res = await HttpUtilService.withdrawal(amount: amount, verificationCode: verificationCode, paymentId: selectedBankItem.value.id!, verificationKey: verificationKey);
    if (res) {
      Get.back();
      selectedBankItem.value = CommonBankCardItemVo(id: 0);
      getWithdrawalRecordList();
      commonPageController.checkLogin();
    }
  }

}